package com.lhf.fvscore.config;

import com.lhf.fvscommon.result.Status;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Indexed;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Response;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Arrays;
import java.util.List;


/**
 * Created by samli on 2017/11/9.
 */
@Configuration
@Indexed
//@Import(value = {ServiceModelToSwagger2MapperImpl.class,
//        ModelMapperImpl.class,
//        ParameterMapperImpl.class,
//        SecurityMapperImpl.class,
//        LicenseMapper.class,
//        VendorExtensionsMapper.class}
//)
@EnableOpenApi
public class SwaggerConfig {

    @Value("${swagger.enable}")
    private boolean enable;
    @Value("${swagger.host}")
    private String host;

    // 访问Swagger需要访问以下
    // http://IP:port/{context-path}/swagger-ui.html
    // 例如本机调试 http://localhost:2626/swagger-ui.html
    @Bean
    public Docket createRestApi() {
//        ParameterBuilder tokenPar = new ParameterBuilder();
//        List<Parameter> pars = Stream.of(
//                tokenPar.name("api_serial_data").description("接口请求流水号").modelRef(new ModelRef("string")).parameterType("header").required(false).build(),
//                tokenPar.name("api_uuid").description("api唯一标识").modelRef(new ModelRef("string")).parameterType("header").required(false).build(),
//                tokenPar.name("api_path").description("第三方接口路径").modelRef(new ModelRef("string")).parameterType("header").required(false).build(),
//                tokenPar.name("api_id").description("接口主建").modelRef(new ModelRef("string")).parameterType("header").required(false).build(),
//                tokenPar.name("api_name").description("接口名称").modelRef(new ModelRef("string")).parameterType("header").required(false).build()
//        ).collect(Collectors.toList());
//
//        pars.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .host(this.host)
                .enable(this.enable)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.lhf.fvsmain.controller"))
                .paths(PathSelectors.any())
                .build()
                .groupName("system")
                .globalResponses(HttpMethod.GET, messages())
                .globalResponses(HttpMethod.POST, messages())
                .globalResponses(HttpMethod.DELETE, messages())
                .globalResponses(HttpMethod.PUT, messages())
                .globalResponses(HttpMethod.PATCH, messages())
                .globalResponses(HttpMethod.TRACE, messages())
                .globalResponses(HttpMethod.OPTIONS, messages())
                .globalResponses(HttpMethod.HEAD, messages())
//                .globalOperationParameters(pars)
                ;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("FVS开发平台开发文档")
                .version("1.0")
                .build();
    }

    public static List<Response> messages() {
        return Arrays.asList(
                new ResponseBuilder().code(String.valueOf(Status.SUCCESS.code)).description(Status.SUCCESS.mes).isDefault(true).build(),
                new ResponseBuilder().code(String.valueOf(Status.FAILURE.code)).description(Status.FAILURE.mes).isDefault(true).build(),
                new ResponseBuilder().code(String.valueOf(Status.ERROR_PASSWORD.code)).description(Status.ERROR_PASSWORD.mes).build(),
                new ResponseBuilder().code(String.valueOf(Status.NO_AUTHORITY.code)).description(Status.NO_AUTHORITY.mes).build(),
                new ResponseBuilder().code(String.valueOf(Status.NO_LOGIN.code)).description(Status.NO_LOGIN.mes).build()
        );
    }


}
